package com.rt.easyjava.base.biz1.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.easyjava.base.biz1.bean.TmptDataSetting;
import com.rt.easyjava.base.biz1.dto.TmptDataSettingDto;
import com.rt.easyjava.base.biz1.mapper.TmptDataSettingMapper;
import com.rt.easyjava.base.biz1.service.intf.TmptDataSettingService;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *  服务实现类
 * @author wuxiaoyong
 * @since 2020-05-01
 */
@Service(value = "tmptDataSettingService")
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class TmptDataSettingServiceImpl extends ServiceImpl<TmptDataSettingMapper, TmptDataSetting> implements TmptDataSettingService {
    private final Logger logger = LoggerFactory.getLogger(TmptDataSettingServiceImpl.class);

    @Autowired
    private MyMapper mapper;
    @Resource
    private TmptDataSettingMapper tmptDataSettingMapper;

    // ==================================================================================
    // ========== 以下使用的是 Mybatis-plus 实现, 适合单表操作 ==============================
    // ==================================================================================

    /**
    * create a object
    * @param tmptDataSettingDto
    * @return
    */
    @Override
    public Result<TmptDataSettingDto> create(TmptDataSettingDto tmptDataSettingDto) {
        Result<TmptDataSettingDto> result = new Result<>();

        // dto to entity
        TmptDataSetting tmptDataSetting = mapper.map(tmptDataSettingDto, TmptDataSetting.class);

        tmptDataSettingMapper.insert(tmptDataSetting);

        return result.success();
    }

    /**
    * update a object by Id
    * @param tmptDataSettingDto
    * @param updateWrapper
    * @return
    */
    @Override
    public Result<TmptDataSettingDto> update(TmptDataSettingDto tmptDataSettingDto, Wrapper updateWrapper) {
        Result<TmptDataSettingDto> result = new Result<>();

        // dto to entity
        TmptDataSetting tmptDataSetting = mapper.map(tmptDataSettingDto, TmptDataSetting.class);

        tmptDataSettingMapper.update(tmptDataSetting, updateWrapper);

        return result.success();
    }

    /**
    * 分页
    * @param page
    * @param queryWrapper
    * @return
    */
    public Result<TmptDataSettingDto> page(Page<TmptDataSetting> page, Wrapper<TmptDataSetting> queryWrapper){
        Result<TmptDataSettingDto> result = new Result<>();

        // mapper
        IPage<TmptDataSetting> iPage = tmptDataSettingMapper.selectPage(page, queryWrapper);

        // 查询结果转换成DTO
        List<TmptDataSettingDto> tmptDataSettingDtoList= mapper.maplist(iPage.getRecords(), TmptDataSettingDto.class);

        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptDataSettingDtoList);

        return result.success();
    }

    /**
    * select all by queryWrapper
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptDataSettingDto> selectAll(Wrapper<TmptDataSetting> queryWrapper) {
        Result<TmptDataSettingDto> result = new Result<>();

        List<TmptDataSetting> list = tmptDataSettingMapper.selectList(queryWrapper);

        List<TmptDataSettingDto> dtoList = mapper.maplist(list, TmptDataSettingDto.class);

        return result.setList(dtoList).success();
    }

    /**
    * select one by id
    * @param id
    * @return
    */
    @Override
    public Result<TmptDataSettingDto> selectById(Long id) {
        Result<TmptDataSettingDto> result = new Result<>();

        TmptDataSetting tmptDataSetting = tmptDataSettingMapper.selectById(id);

        TmptDataSettingDto tmptDataSettingdto = mapper.map(tmptDataSetting, TmptDataSettingDto.class);

        return result.setObj(tmptDataSettingdto).success();
    }




    // ==================================================================================
    // ========== 以下使用的是自定义的 mapper.xml 实现, 适合多表查询 =========================
    // ==================================================================================

    /**
    * 分页查询：mybatis plus 自定义sql分页查询
    * @param page
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptDataSettingDto> myPage(Page<TmptDataSetting> page, Wrapper<TmptDataSetting> queryWrapper) {
        Result<TmptDataSettingDto> result = new Result();

        IPage<TmptDataSetting> iPage = tmptDataSettingMapper.myPage(page, queryWrapper);

        //查询结果转换成DTO
        List<TmptDataSettingDto> tmptDataSettingDtoList = mapper.maplist(iPage.getRecords(), TmptDataSettingDto.class);

        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptDataSettingDtoList);

        return result.success();
    }

    @Override
    public Result<TmptDataSettingDto> mySelectBy(Wrapper<TmptDataSetting> queryWrapper) {
        Result<TmptDataSettingDto> result = new Result();

        List<TmptDataSetting> list = tmptDataSettingMapper.mySelectBy(queryWrapper);
        List<TmptDataSettingDto> dtoList = mapper.maplist(list, TmptDataSettingDto.class);
        result.setList(dtoList);

        return result.success();
    }

    @Override
    public Result<TmptDataSettingDto> mySelectById(long id) {
        Result<TmptDataSettingDto> result = new Result();

        TmptDataSetting tmptDataSetting = tmptDataSettingMapper.mySelectById(id);
        TmptDataSettingDto dto = mapper.map(tmptDataSetting,TmptDataSettingDto.class);
        result.setObj(dto);

        return result.success();
    }



}
